package com.hmall.search.web;

import com.hmall.common.client.ItemClient;
import com.hmall.common.dto.Item;
import com.hmall.common.dto.PageDTO;
import com.hmall.search.pojo.ItemDoc;
import com.hmall.search.pojo.RequestParams;
import com.hmall.search.service.ISearchService;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("search")
public class SearchController {
    @Autowired
    private ItemClient itemClient;

    @Autowired
    private ISearchService searchService;

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    //自动补全：请求路径为search/suggestion?key=x
    @GetMapping("/suggestion")
    public List<String> getSuggestion(@RequestParam("key") String prefix) throws IOException {
        return searchService.getSuggestion(prefix);
    }

    //查询过滤项:请求路径为search/suggestion/filters
    @PostMapping("/filters")
    public Map<String,List<String>> getFilters(@RequestBody RequestParams requestParams) throws IOException {//实体类使用RequestBody
        return searchService.getFilters(requestParams);
    }

    //基本搜索功能
    @PostMapping("/list")
    //分页结果 ItemDoc
    public PageDTO<ItemDoc> searchItem(@RequestBody RequestParams requestParams) throws IOException {
        return searchService.getSearch(requestParams);
    }

}
